from django.db import models

# Create your models here.

class StoreSalesOrder(models.Model):
    order_id = models.CharField(db_index=True)  # 添加索引
    order_date = models.DateField(db_index=True)  # 添加索引
    store_code = models.CharField()
    retail_price = models.DecimalField(max_digits=20, decimal_places=4)
    transaction_price = models.DecimalField(max_digits=20, decimal_places=4)
    created_at = models.DateTimeField(auto_now_add=True)
    cancel_at = models.DateTimeField(null=True, blank=True)
    finished_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        indexes = [
            models.Index(fields=['order_date', 'order_id']),  # 复合索引
        ]


class StoreSalesOrderDetail(models.Model):
    order_id = models.CharField(db_index=True)  # 添加索引
    product_code = models.CharField(db_index=True)  # 添加索引
    activity_code = models.CharField(null=True, blank=True)
    member_code = models.CharField(null=True, blank=True)
    payment_code = models.CharField(null=True, blank=True)
    discount_off_rate = models.FloatField(null=True, blank=True)
    quantity = models.FloatField()

    class Meta:
        indexes = [
            models.Index(fields=['order_id', 'product_code']),  # 复合索引
        ]


class StoreProductReplenishment(models.Model):
    order_id = models.CharField(db_index=True)  # 添加索引
    order_date = models.DateField(db_index=True)  # 添加索引
    product_code = models.CharField(db_index=True)  # 添加索引
    order_quantity = models.FloatField()

    avg_sales_quantity = models.FloatField(null=True, blank=True)
    safety_inventory_quantity = models.FloatField(null=True, blank=True)
    instock_quantity = models.FloatField(null=True, blank=True)
    dynamic_sales_days = models.FloatField(null=True, blank=True)
    safety_inventory_days = models.FloatField(null=True, blank=True)

    created_at = models.DateTimeField(auto_now_add=True)
    is_cancelled = models.BooleanField(default=False)
    cancel_at = models.DateTimeField(null=True, blank=True)
    finished_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        indexes = [
            models.Index(fields=['order_date', 'product_code']),  # 复合索引
            models.Index(fields=['order_id', 'product_code']),  # 复合索引
        ]


class StoreReplenishment(models.Model):
    order_id = models.CharField(db_index=True)  # 添加索引
    order_date = models.DateField(db_index=True)  # 添加索引
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        indexes = [
            models.Index(fields=['order_date', 'order_date']),  # 复合索引
        ]


